import 'package:mirror/CommonLoading.dart';
import 'package:mirror/Util.dart';
import 'package:mirror/bean/UserBean.dart';
import 'package:mirror/service/CacheService.dart';
import 'package:mirror/service/NetworkService.dart';

class UserApi {
  static const _loginUrl = "Users-Service/users/login";
  static const _registerUrl = "Users-Service/users/register";

  static const _updateProfileUrl = "Users-Service/users/revise";
  static const String _uploadUrl = 'App-Service/upload/avatar';

  static Future login(
      {required String phone,
      required String password,
      required String email}) async {
    var res = await NetworkService.post(
        url: _loginUrl,
        map: {"passwd": password, "userName": phone, "userEmail": email});
    var a = res.data;
    a['password'] = password;
    await saveUser(a);
  }

  static Future<void> saveUser(res) async {
    await CacheService.putMap("user", res);
  }

  static Future register(
      {required String phone, required String password}) async {
    await NetworkService.post(
        url: _registerUrl, map: {"passwd": password, "phone": phone});
  }

  static Future<UserBean?> getUser() async {
    var user = await CacheService.getMap("user");
    if (user == null) return null;
    return UserBean.fromJson(user);
  }

//邮箱，头像后面再考虑
  static updateProfile(
      {String? nickName,
      String? userHobby,
      String? userBirthday,
      String? avatar}) async {
    String uid = "${(await getUser())?.userUid}";
    if (uid.isNotEmpty && uid != 'null') {
      Map<String, dynamic> map = {};
      map['userUid'] = uid;
      if (nickName != null) {
        map['nickName'] = nickName;
      }
      if (avatar != null) {
        map['userAvatar'] = avatar;
      }

      if (userHobby != null) {
        map['userHobby'] = userHobby;
      }
      if (userBirthday != null) {
        map['userBirthday'] = userBirthday;
      }
      var res = await NetworkService.post(url: _updateProfileUrl, map: map);
      print(res);
    } else {
      Util.toast("用户信息获取失败");
    }
  }

  static Future<String?> changeAvatar({required String path}) async {
    String uid = "${(await getUser())?.userUid}";
    try {
      CommonLoading.show();
      final String result =
          await NetworkService.upload('$_uploadUrl/$uid', path);
      CommonLoading.hide();
      return result;
    } catch (e) {
      print(e);
      CommonLoading.hide();
      return null;
    }
  }

  static updateLocal(
      {String? nickName,
      String? userHobby,
      String? userBirthday,
      String? avatar}) async {
    var user = await getUser();

    if (user != null) {
      var userMap = user.toJson();
      if (nickName != null) {
        userMap['nickName'] = nickName;
      }
      if (userHobby != null) {
        userMap['userHobby'] = userHobby;
      }
      if (userBirthday != null) {
        userMap['userBirthday'] = userBirthday;
      }
      if (avatar != null) {
        userMap['avatar'] = avatar;
      }
      await saveUser(userMap);
    } else {
      Util.toast("更新本地信息出错，用户信息获取失败");
    }
  }
}
